/*
 * 描述： 交易金額信息
 * 修改人： Weng.weng
 * 修改时间： 2021年8月24日
 * 项目： amount-allocation-demo
 */
package com.tuozixuan.allocation;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import com.tuozixuan.allocation.amount.Amount;
import com.tuozixuan.allocation.amount.Commission;
import com.tuozixuan.allocation.amount.Interest;
import com.tuozixuan.allocation.amount.OverdueInterest;
import com.tuozixuan.allocation.amount.Principal;
import com.tuozixuan.allocation.constant.AmountType;

/**
 * 交易金額信息<br>
 * 
 * @author Weng.weng
 * @version [版本号, 2021年8月24日]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
public class TxAmount {
	
	/** 本金 */
	private Principal principal;
	
	/** 利息 */
	private Interest interest;
	
	/** 逾期利息 */
	private OverdueInterest overdueInterest;
	
	/** 佣金 */
	private Commission commission;
	
	/** 剩餘金額 */
	private BigDecimal remainAmount;
	
	/** 豁免相關金額 */
	private Map<AmountType, BigDecimal> exemptAmountMap;
	/**
	public TxAmount(List<Amount> amountList, BigDecimal remainAmount, Map<AmountType, BigDecimal> exemptAmountMap) {
		this.remainAmount = remainAmount;
		this.exemptAmountMap = exemptAmountMap;
		for (Amount amount : amountList) {
			switch (amount.getType()) {
			case PRINCIPAL:
				this.principal = (Principal) amount;
				break;
			case INTEREST:
				this.interest = (Interest) amount;
				break;
			case OVERDUE_INTEREST:
				this.overdueInterest = (OverdueInterest) amount;
				break;
			case COMMISSION:
				this.commission = (Commission) amount;
				break;
			default:
				break;
			}
		}
	}*/
	
	public TxAmount(Map<AmountType, Amount> amountMap, BigDecimal remainAmount, Map<AmountType, BigDecimal> exemptAmountMap) {
		this.remainAmount = remainAmount;
		this.exemptAmountMap = exemptAmountMap;
		
		amountMap.values().stream().forEach(model -> {
			BigDecimal remain = model.getTotal().subtract(Optional.ofNullable(model.getActual()).orElse(BigDecimal.ZERO)).add(Optional.ofNullable(model.getExempt()).orElse(BigDecimal.ZERO));
			model.setRemain(remain);
		});
		
		for (Amount amount : amountMap.values()) {
			switch (amount.getType()) {
			case PRINCIPAL:
				this.principal = (Principal) amount;
				break;
			case INTEREST:
				this.interest = (Interest) amount;
				break;
			case OVERDUE_INTEREST:
				this.overdueInterest = (OverdueInterest) amount;
				break;
			case COMMISSION:
				this.commission = (Commission) amount;
				break;
			default:
				break;
			}
		}
	}

	public Principal getPrincipal() {
		return principal;
	}

	public void setPrincipal(Principal principal) {
		this.principal = principal;
	}

	public Interest getInterest() {
		return interest;
	}

	public void setInterest(Interest interest) {
		this.interest = interest;
	}

	public OverdueInterest getOverdueInterest() {
		return overdueInterest;
	}

	public void setOverdueInterest(OverdueInterest overdueInterest) {
		this.overdueInterest = overdueInterest;
	}
	
	public Commission getCommission() {
		return commission;
	}

	public void setCommission(Commission commission) {
		this.commission = commission;
	}

	public BigDecimal getRemainAmount() {
		return remainAmount;
	}

	public void setRemainAmount(BigDecimal remainAmount) {
		this.remainAmount = remainAmount;
	}
	
	public Map<AmountType, BigDecimal> getExemptAmountMap() {
		return exemptAmountMap;
	}

	public void setExemptAmountMap(Map<AmountType, BigDecimal> exemptAmountMap) {
		this.exemptAmountMap = exemptAmountMap;
	}

	@Override
	public String toString() {
		return "TxAmount [principal=" + principal + ", interest=" + interest + ", overdueInterest=" + overdueInterest + ", commission=" + commission
				+ ", remainAmount=" + remainAmount + ", exemptAmountMap=" + exemptAmountMap + "]";
	}
}
